﻿using Ross.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
using Ross.EntityFramework.Model;
using Ross.Service;

namespace Ross.BSCSystem.Controllers
{
    [SimpleAuthenticationFilter]
    public class UsersController : BaseApiController
    {
        /// <summary>
        /// 获取用户信息，但不含密码.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult<ResultDto<Service.Dto.oUser>> Get()
        {
            ResultDto<Service.Dto.oUser> result = new ResultDto<Service.Dto.oUser>();
            using (UserInfoService serv = new UserInfoService(dbContextStr))
            {
                result = serv.Get();
            }
            return Json(result);
        }

        /// <summary>
        /// 插入信息.
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult<ResultDto<bool>> Insert([FromBody] Users vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UsersService serv = new UsersService(dbContextStr))
            {
                result = serv.Create(vm);
            }
            return Json(result);
        }

        [HttpGet]
        public JsonResult<ResultDto<List<Service.Dto.oUser>>> GetPageList(string username = "", string empid = "", string role = "", int pageindex = 1, int pagesize = 10)
        {
            ResultDto<List<Service.Dto.oUser>> result = new ResultDto<List<Service.Dto.oUser>>();
            using (UsersService serv = new UsersService(dbContextStr))
            {
                result = serv.GetPageList(pageindex, pagesize, username, empid, role);
            }
            return Json(result);
        }

        /// <summary>
        /// 更新个人信息.
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult<ResultDto<bool>> Update([FromBody] Service.Dto.oUser vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UsersService serv = new UsersService(dbContextStr))
            {
                result = serv.Update(vm);
            }
            return Json(result);
        }

        /// <summary>
        /// 更改密码.
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult<ResultDto<bool>> ChangePsw([FromBody] Service.Dto.UptPsw vm)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UsersService serv = new UsersService(dbContextStr))
            {
                result = serv.UpdatePsw(vm);
            }
            return Json(result);
        }

        /// <summary>
        /// 重置密码.
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult<ResultDto<bool>> ResetPsw(int userId, string password)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UsersService serv = new UsersService(dbContextStr))
            {
                result = serv.ResetPsw(userId, password);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除用户.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult<ResultDto<bool>> Delete(int id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            using (UsersService serv = new UsersService(dbContextStr))
            {
                result = serv.Delete(id);
            }
            return Json(result);
        }

        /// <summary>
        /// 获取整个用户列表.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult<ResultDto<List<Service.Dto.oUser>>> GetList()
        {
            ResultDto<List<Service.Dto.oUser>> result = new ResultDto<List<Service.Dto.oUser>>();
            using (UsersService serv = new UsersService(dbContextStr))
            {
                result = serv.GetAllList();
            }
            return Json(result);
        }

        /// <summary>
        /// 通过ID查询.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult<ResultDto<Service.Dto.oUser>> GetByID(int id)
        {
            ResultDto<Service.Dto.oUser> result = new ResultDto<Service.Dto.oUser>();
            using (UsersService serv = new UsersService(dbContextStr))
            {
                result = serv.GetByID(id);
            }
            return Json(result);
        }
    }
}